<?php
class Ledger extends Admin_Controller
{
	
	function __construct()
	{
		parent::__construct();
		$this->load->model('fund_model','fund');
		$this->load->model('voucher_model','voucher');
		$this->load->model('voucher_balance_model','balance');
		$this->load->model('voucher_check_model','check');
		$this->load->model('voucher_receive_paid_model','receive_paid');
		$this->load->model('voucher_detail_model','voucher_detail');
	}
	
	//**** บัญชีแยกประเภท ******
	function index($fund_id)
	{
		//$this->db->debug = TRUE;
		//$data['sum'] = 32371338.31;
		//$data['sum'] = 0;
		$data['fund'] = $this->fund->get_row($fund_id);
		$user = login_data();
		$agency_id = (level_data('view'))?(isset($_GET['agency_id']))?$_GET['agency_id']:$user['agency_id']:$user['agency_id'];
		//**************Start where section ******************
		$where = '';
		list($year,$month,$data['year'],$data['month']) = last_voucher_date($fund_id);
		$data['agency_id'] = $agency_id;
		list($start_date,$end_date) = year_budget($year);
		$where = ' and vouchers.voucher_date between \''.$start_date.'\' and \''.$end_date.'\'';
		$balance_where = ' and YEAR(vouchers.voucher_date) = '.($year-1).' and MONTH(vouchers.voucher_date) = 10';
		if(!empty($month))
		{
			$where = ' and YEAR(vouchers.voucher_date) = '.$year.' and MONTH(vouchers.voucher_date) = '.$month;
			$balance_where = ' and YEAR(vouchers.voucher_date) = '.$year.' and MONTH(vouchers.voucher_date) = '.$month;
		}
		if(!empty($agency_id))$where .= ' and vouchers.agency_id = '.$agency_id;
		//**************End where section ******************
		
		$data['dr'] = 0;
		$data['cr'] = 0;
		$account_id = (@$_GET['account_id'])?$_GET['account_id']:$this->db->getOne('select min(id) from accounts where fund_id = '.$fund_id);
		$data['account_id'] = $account_id;
		$account = $this->db->getrow('select * from voucher_details 
		where account_id = '.$account_id.' 
		and voucher_id = (select id from vouchers where is_balance = 1 and mode is null and fund_id = '.$fund_id.' and agency_id = '.$agency_id.$balance_where.')');
		if($account)
		{
			$data['sum'] = $account['PRICE'];
			if($account['TYPE']=='cr')$data['sum'] = $data['sum'] * -1;
		}
		else
		{
			$data['sum'] = 0;
		}
		$data['result'] = $this->voucher_detail->get('
		select voucher_details.*,vouchers.voucher_date,vouchers.number,accounts.name account_name,accounts.code account_code,
		(
		CASE when voucher_details.price <> (select sum(price) from voucher_details a where a.voucher_id = voucher_details.voucher_id and a."TYPE" <> voucher_details."TYPE") 
		THEN voucher_details.price 
		ELSE (select price from voucher_details a where a.account_id = '.$account_id.' and  a.voucher_id = voucher_details.voucher_id and a."TYPE" <> voucher_details."TYPE") 
		END
		)AS real_price
		from voucher_details 
		left join vouchers 
		on voucher_details.voucher_id = vouchers.id 
		left join accounts
		on voucher_details.account_id = accounts.id 
		where  
		vouchers.fund_id = '.$fund_id.' 
		and vouchers.is_balance is null
		and vouchers.mode is null 
		and voucher_details.voucher_id in (select voucher_id from voucher_details where account_id = '.$account_id.')
		and voucher_details.account_id <> '.$account_id.' 
		and voucher_details."TYPE" <> (select "TYPE" from voucher_details a where a.account_id = '.$account_id.' and a.voucher_id = voucher_details.voucher_id)
		'.$where.'
		order by vouchers.voucher_date asc,voucher_details.voucher_id asc
		',FALSE);
		$this->template->append_metadata(js_print());
		$this->template->build('voucher_ledger',$data);
	}
	
	
	
}
?>